using System.Drawing;
using System.Windows.Forms;
using DrawTools.Editor.CanvasFigures;

namespace DrawTools.Editor.Tools.States
{
  internal class MoveObjectState : IPointerToolState
  {
    private readonly IDrawingCanvas _canvas;
    private Point _lastPoint;

    public MoveObjectState(IDrawingCanvas canvas, CanvasObject canvasObject, Point lastPoint)
    {
      _canvas = canvas;

      // Deselect all if Ctrl is not pressed and clicked object is not selected yet
      if ((Control.ModifierKeys & Keys.Control) == 0 && !canvasObject.IsSelected)
        _canvas.GraphicsList.DeselectAll();

      // Select clicked object
      canvasObject.IsSelected = true;

      _canvas.SetCursor(Cursors.SizeAll);

      _lastPoint = lastPoint;
    }

    #region IPointerToolState Members

    public void Update(Point point)
    {
      int dx = point.X - _lastPoint.X;
      int dy = point.Y - _lastPoint.Y;

      _lastPoint.X = point.X;
      _lastPoint.Y = point.Y;

      foreach (CanvasObject selectionedObject in _canvas.GraphicsList.SelectedObjects)
      {
        selectionedObject.Move(dx, dy);
      }

      _canvas.SetCursor(Cursors.SizeAll);
      _canvas.Refresh();
    }

    public void Release()
    {
      _lastPoint = new Point(0, 0);
    }

    #endregion
  }
}